package com.hzy.alibaba.config;

import com.hzy.alibaba.enums.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @author abc
 * @description 动态数据源，每执行一次数据库，动态获取数据源
 */
@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        DataSourceType dataSourceKey = DynamicDataSourceContextHolder.getDataSourceKey();
        log.info("Switch DataSource to [{}] in Method [{}]", dataSourceKey.name(), "determineCurrentLookupKey");
        return dataSourceKey;
    }
}
